<?php
/**
 * @author 	Vu Nhat Lam
 * 			Mail: lamvnd@gmail.com
 * 			Mobile: 0984 52 62 68
 * @version 1.0.0
 */
error_reporting(0);
ignore_user_abort(true);
@set_time_limit(0);

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

$torrents = $db->query_read("
	SELECT torrentid
	FROM " . TABLE_PREFIX . "torrent
");
while ($torrent = $db->fetch_array($torrents))
{
	if ($torrent['torrentid'])
	{
		// Count seeder
		$seeders = $db->query_first("
			SELECT COUNT(*) AS countseeders
			FROM " . TABLE_PREFIX . "torrentpeers
			WHERE torrentid = {$torrent['torrentid']}
				AND `left` = 0
		");
		$seeders = intval($seeders['countseeders']);

		// Count leecher
		$leechers = $db->query_first("
			SELECT COUNT(*) AS countleechers
			FROM " . TABLE_PREFIX . "torrentpeers
			WHERE torrentid = {$torrent['torrentid']}
				AND `left` <> 0
		");
		$leechers = intval($leechers['countleechers']);

		// Update
		$db->query_write("
			UPDATE " . TABLE_PREFIX . "torrent
			SET seeders = $seeders,
				leechers = $leechers
			WHERE torrentid = {$torrent['torrentid']}
		");
	}
}

echo '<p>Reload Counter <b>DONE</b></p>';
